package com.cytech.cloudclinic.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.utils.RequestWithViewUtils;
import com.yechaoa.yutils.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ForgetPasswordDialog extends DialogFragment {

    @BindView(R.id.et_forget_psw_account)
    EditText etForgetPswAccount;
    @BindView(R.id.et_forget_psw_message_code)
    EditText etForgetPswMessageCode;
    @BindView(R.id.et_forget_psw_password)
    EditText etForgetPswPassword;
    @BindView(R.id.btn_forget_psw_get_message)
    Button btnForgetPswGetMessage;

    private MyCount mCountThread;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_forget_password, null);
        ButterKnife.bind(this, view);
        builder.setView(view);
        return builder.create();
    }

    @OnClick({R.id.iv_dialog_forget_psw_cancel})
    public void dismissDialog(View view) {
        dismiss();
    }

    @OnClick(R.id.btn_forget_psw_get_message)
    public void getMessageCode(View v){
        String userAccount = etForgetPswAccount.getText().toString();
        if(TextUtils.isEmpty(userAccount)){
            ToastUtil.showToast("请输入登录账号");
            return;
        }
        JSONObject json = new JSONObject();
        try {
            json.put("phone",userAccount);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        RequestWithViewUtils.getInstance().addDisposable(RequestWithViewUtils.getInstance().apiServer.getMessageCode(body), new BaseObserver<BaseBean<Object>>(null, false) {
            @Override
            public void onSuccess(BaseBean<Object> o) {
                mCountThread = new MyCount(60 * 1000, 1000);
                mCountThread.start();
            }

            @Override
            public void onError(String msg) {

            }
        });
    }


    @OnClick(R.id.btn_forget_psw_get_sure)
    public void changePassword(View v){
        String userAccount = etForgetPswAccount.getText().toString();
        String code = etForgetPswMessageCode.getText().toString();
        String password = etForgetPswPassword.getText().toString();
        if(TextUtils.isEmpty(userAccount)){
            ToastUtil.showToast("请输入登录账号");
            return;
        }
        if(TextUtils.isEmpty(code)){
            ToastUtil.showToast("请输入短信验证码");
            return;
        }
        if(TextUtils.isEmpty(password)){
            ToastUtil.showToast("请输入新密码");
            return;
        }
        JSONObject json = new JSONObject();
        try {
            json.put("phone",userAccount);
            json.put("code",code);
            json.put("password",password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        RequestWithViewUtils.getInstance().addDisposable(RequestWithViewUtils.getInstance().apiServer.updatePassword(body), new BaseObserver<BaseBean<Object>>(null, false) {
            @Override
            public void onSuccess(BaseBean<Object> o) {
                ToastUtil.showToast("密码修改成功，请登录");
                dismiss();
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    /* 定义一个倒计时的内部类 */
    class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            // 倒计时结束
            countFinish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            btnForgetPswGetMessage.setClickable(false);
            btnForgetPswGetMessage.setText(millisUntilFinished / 1000 + "S");
        }
    }

    /**
     * 倒计时结束
     */
    private void countFinish() {
        btnForgetPswGetMessage.setText("重新获取");
        btnForgetPswGetMessage.setClickable(true);

        //mGetCodeBtn.setTextColor(ContextCompat.getColor(LookForPswPhoneActivity.this,R.color.orange));
        if (mCountThread != null)
            mCountThread.cancel();
    }
}
